package com.frejoys.common.util;

import cn.hutool.core.util.ZipUtil;
import com.frejoys.common.config.ValueConfig;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.io.*;

@Component
@RequiredArgsConstructor
public class FileUtil {

    private final ValueConfig valueConfig;

    //创建文件
    public String uploadFile(File[] files, String batchCode) throws IOException {
return "";
        // File destination = new File(valueConfig.getLocalFilePath() + batchCode);
        // if (!destination.exists()) {
        //     destination.mkdirs();
        // }
        //
        // for (File file : files) {
        //     File destFile = new File(destination, file.getName());
        //     FileInputStream fileInputStream = new FileInputStream(file);
        //     FileOutputStream fileOutputStream = new FileOutputStream(destFile);
        //     byte[] bytes = new byte[1024];
        //     int length;
        //     while ((length = fileInputStream.read(bytes)) > 0) {
        //         fileOutputStream.write(bytes, 0, length);
        //     }
        //     fileInputStream.close();
        //     fileOutputStream.close();
        //
        // }
        // System.out.println("文件上传完成: " + destination.getAbsolutePath());
        // //压缩文件
        // String zipFilePath = createZipFile(destination);
        // //删除文件
        // deleteFile(destination);
        // return zipFilePath;
    }


    //压缩文件
    public String createZipFile(File folder) throws IOException {
        String zipFilePath = folder.getAbsolutePath() + ".zip";
        if (folder.isDirectory()) {
            ZipUtil.zip(folder.getAbsolutePath(), zipFilePath, true);
        }
        // System.out.println("文件压缩完成: " + zipFilePath);
        return zipFilePath;
    }

    //删除文件
    public static void deleteFile(File folder) {

        if (cn.hutool.core.io.FileUtil.del(folder)) {
            System.out.println("文件夹删除成功: " + folder.getAbsolutePath());
        } else {
            System.out.println("无法删除文件夹: " + folder.getAbsolutePath());
        }
    }

}
